package ui.troopmanager.mooresr.us;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

import troopmanager.mooresr.us.DataManager;



public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton peopleButton;
	private JButton eventsButton;
	private JButton vehiclesButton;
	private JLabel statusLabel;
	private static PeopleDialog peopleDialog;
	private static EventDialog eventDialog;
	private static VehicleDialog vehicleDialog;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	private static MainFrame instance;
	
	
	public MainFrame() {
		initComponents();
	}

	private void initComponents() {

		setLayout(new BorderLayout());
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
		add(centerPanel, BorderLayout.CENTER);
		centerPanel.add(getPeopleButton());
		centerPanel.add(getEventButton());
		centerPanel.add(getVehicleButton());
		add(getStatusLabel(), BorderLayout.PAGE_END);
		setSize(320, 240);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
	
	public static MainFrame getInstance() {
		return instance;
	}

	private JLabel getStatusLabel() {
		if (statusLabel == null) {
			statusLabel = new JLabel();
		}
		return statusLabel;
	}


	private JButton getVehicleButton() {
		if (vehiclesButton == null) {
			vehiclesButton = new JButton();
			vehiclesButton.setText("Vehicles");
			vehiclesButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					vehiclesButtonActionActionPerformed(event);
				}
			});
		}
		return vehiclesButton;
	}

	private JButton getEventButton() {
		if (eventsButton == null) {
			eventsButton = new JButton();
			eventsButton.setText("Events");
			eventsButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					eventsButtonActionActionPerformed(event);
				}
			});
		}
		return eventsButton;
	}

	private JButton getPeopleButton() {
		if (peopleButton == null) {
			peopleButton = new JButton();
			peopleButton.setText("People");
			peopleButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					peopleButtonActionActionPerformed(event);
				}
			});
		}
		return peopleButton;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}


	public static void main(String[] args) {
		installLnF();
		


		/* Create the main frame, set the cursor to an hourglass, and disable all
		 * buttons.  This is the initial state of the app until the database connection
		 * has been made.  The worker thread below will connect to the database, then
		 * enable everything once it's ready to run.
		 */
		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				instance = new MainFrame();
				instance.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
				instance.setTitle("Troop Records");
				instance.getContentPane().setPreferredSize(instance.getSize());
				instance.pack();
				instance.setLocationRelativeTo(null);
				instance.setVisible(true);
				
				Cursor hourglass = new Cursor(Cursor.WAIT_CURSOR);
				instance.setCursor(hourglass);
				instance.statusLabel.setText("Initializing...");
				instance.peopleButton.setEnabled(false);
				instance.eventsButton.setEnabled(false);
				instance.vehiclesButton.setEnabled(false);
			}
		});
		
		/*
		 * This worker thread calls dm.init() to try to connect to the database.
		 * Since this takes some time the app starts up with an hourglass cursor
		 * and with everything disabled.  Once dm.init() returns the thread will
		 * enable everything and set the cursor back to normal.
		 */
		
		SwingWorker<Integer, Void> worker = new SwingWorker<Integer, Void>() {
			@Override
			public Integer doInBackground() {
				int result;
				
				
				String password = (String)JOptionPane.showInputDialog(
				                    "Enter the database password");

				
				DataManager dm = DataManager.getInstance();
				result = dm.init("cmoore", password);
				
				MainFrame frame = MainFrame.getInstance();
				if (result == 0) {
					frame.statusLabel.setText("Ready");
					frame.peopleButton.setEnabled(true);
					frame.eventsButton.setEnabled(true);
					frame.vehiclesButton.setEnabled(true);
					Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);
					frame.setCursor(normal);
					

					
				} else {
					frame.statusLabel.setText("Can't connect to database");
				}
				
				return result;
				
			}
			
			@Override
			public void done() {
				// Create the three main subforms
				// This has to be done here, after the DB is open, because some
				// dialogs depend on the DB at creation time.
				
				// TODO: Check that the db actually was opened
				
				
				peopleDialog = new PeopleDialog();
				peopleDialog.setLocationRelativeTo(instance);
				
				eventDialog = new EventDialog();
				eventDialog.setLocationRelativeTo(instance);

				vehicleDialog = new VehicleDialog();
				vehicleDialog.setLocationRelativeTo(instance);

			}
		};
		
		
		worker.execute();
		
		
	}

	private void peopleButtonActionActionPerformed(ActionEvent event) {
		peopleDialog.reloadData();
		peopleDialog.setVisible(true);
	}

	private void eventsButtonActionActionPerformed(ActionEvent event) {
		eventDialog.reloadData();
		eventDialog.setVisible(true);
	}

	private void vehiclesButtonActionActionPerformed(ActionEvent event) {
		vehicleDialog.reloadData();
		vehicleDialog.setVisible(true);
	}

}
